#!/bin/sh

die () { echo "$@" ; cleanup ; exit 1; }

cleanup() {
  unset -f nvm_ls_remote nvm_ls_remote_iojs
}

: nvm.sh
\. ../../../nvm.sh

\. ../../common.sh

try_err nvm_remote_versions stable
EXPECTED_OUTPUT="Implicit aliases are not supported in nvm_remote_versions."
[ "_$CAPTURED_STDERR" = "_$EXPECTED_OUTPUT" ] || die "implicit alias 'stable' did not error out with correct message, got $CAPTURED_STDERR"
[ "_$CAPTURED_EXIT_CODE" = "_1" ] || die "implicit alias 'stable' did not exit with code 1, got $CAPTURED_EXIT_CODE"

try_err nvm_remote_versions unstable
EXPECTED_OUTPUT="Implicit aliases are not supported in nvm_remote_versions."
[ "_$CAPTURED_STDERR" = "_$EXPECTED_OUTPUT" ] || die "implicit alias 'unstable' did not error out with correct message, got $CAPTURED_STDERR"
[ "_$CAPTURED_EXIT_CODE" = "_1" ] || die "implicit alias 'unstable' did not exit with code 1, got $CAPTURED_EXIT_CODE"

nvm_ls_remote() {
  echo "N/A"
}
try nvm_remote_versions foo
[ "_$CAPTURED_STDOUT" = "_N/A" ] || die "nonexistent version did not report N/A"
[ "_$CAPTURED_EXIT_CODE" = "_3" ] || die "nonexistent version did not exit with code 3, got $CAPTURED_EXIT_CODE"

nvm_ls_remote_iojs() {
  echo "N/A"
}
try nvm_remote_versions iojs-foo
[ "_$CAPTURED_STDOUT" = "_N/A" ] || die "nonexistent version did not report N/A"
[ "_$CAPTURED_EXIT_CODE" = "_3" ] || die "nonexistent version did not exit with code 3, got $CAPTURED_EXIT_CODE"


nvm_ls_remote() {
  echo "test output"
  echo "more test output"
  echo "pattern received: _$1_"
}
nvm_ls_remote_iojs() {
  echo "test iojs output"
  echo "more iojs test output"
  echo "iojs pattern received: _$1_"
}

try nvm_remote_versions foo
[ "_$CAPTURED_STDOUT" = "_$(nvm_ls_remote foo)
$(nvm_ls_remote_iojs foo)" ] \
  || die "nvm_remote_versions foo did not return contents of nvm_ls_remote foo combined with nvm_ls_remote_iojs foo; got $CAPTURED_STDOUT"
[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die "nvm_remote_versions foo did not exit with 0, got $CAPTURED_EXIT_CODE"

try nvm_remote_versions node
[ "_$CAPTURED_STDOUT" = "_$(nvm_ls_remote)" ] \
  || die "nvm_remote_versions node did not return contents of nvm_ls_remote; got $CAPTURED_STDOUT"
[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die "nvm_remote_versions node did not exit with 0, got $CAPTURED_EXIT_CODE"

try nvm_remote_versions iojs-foo
[ "_$CAPTURED_STDOUT" = "_$(nvm_ls_remote iojs-foo)
$(nvm_ls_remote_iojs iojs-foo)" ] \
  || die "nvm_remote_versions iojs-foo did not return contents of nvm_ls_remote iojs-foo combined with nvm_ls_remote_iojs iojs-foo; got $CAPTURED_STDOUT"
[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die "nvm_remote_versions iojs-foo did not exit with 0, got $CAPTURED_EXIT_CODE"

try nvm_remote_versions iojs
[ "_$CAPTURED_STDOUT" = "_$(nvm_ls_remote_iojs)" ] \
  || die "nvm_remote_versions iojs did not return contents of nvm_ls_remote_iojs; got $CAPTURED_STDOUT"
[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die "nvm_remote_versions iojs did not exit with 0, got $CAPTURED_EXIT_CODE"

cleanup
